<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2016-2017 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 公告
 * @author  Xmh
 */
class Announce {

    private $AnnounceModel;

    public function __construct() {
        $this->AnnounceModel = M("Announcement");
    }

    /**
     * 获取公告列表
     * @param type $page
     * @param type $size
     * @param type $params
     * @return array
     */
    public function get_list($page, $size, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $size = (int) $size < 1 ? 20 : (int) $size;
        $map = array();
        if(!empty($params['title'])){
            $map['title'] = array('like',"%".$params['title']."%");
        }
        $cols = "ann_id,title,content,emp_author,createtime";
        $total = $this->AnnounceModel->field($cols)
                ->where($map)
                ->count();
        $list = $this->AnnounceModel->field($cols)
                ->where($map)
                ->limit($size)
                ->page($page)
                ->order('createtime DESC')
                ->select();
        array_map('strip_tags',$list);
        $result = array(
            'list'=> $list,
            'total'=>$total
        );
        return $result;
    }
    /**
     * 获取公告详情
     * @param type $ann_id
     * @return type
     */
    public function get_info($ann_id) {
        if (empty($ann_id)) {
            AuctionException::make_throw('100056');
        }
        $cols = 'title,content,createtime';
        $res = $this->AnnounceModel->field($cols)->find($ann_id);
        return $res;
    }
    /**
     * 获取公告详情
     * @param int $id
     *
     * @return array
     */
    public function get_announce($id) {
        return $this->AnnounceModel->where(array('ann_id' => $id))->find();
    }
    /**
     * 编辑公告
     */
    public function save_announce($ann_id,$data = array()) {
        return $this->AnnounceModel->where(array('ann_id'=>$ann_id))->save($data);
    }
}
